package com.example.tablist_tab.slice;

import com.example.tablist_tab.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.TabList;
import ohos.agp.components.Text;

public class MainAbilitySlice extends AbilitySlice {
    private Text text_title;
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);

        text_title = (Text) findComponentById(ResourceTable.Id_text_title);

        TabList tabList = (TabList) findComponentById(ResourceTable.Id_tab_list);
        TabList.Tab tab = tabList.new Tab(getContext());
        tab.setText("推荐");
        tabList.addTab(tab);
        TabList.Tab tab2 = tabList.new Tab(getContext());
        tab2.setText("视频");
        tabList.addTab(tab2);
        TabList.Tab tab3 = tabList.new Tab(getContext());
        tab3.setText("新闻");
        tabList.addTab(tab3);
        TabList.Tab tab4 = tabList.new Tab(getContext());
        tab4.setText("问答");
        tabList.addTab(tab4);


        // 新增选项
        // 本示例中在"推荐"和"视频"之间的页签中新增"资源"页签
        TabList.Tab tab5 = tabList.new Tab(getContext());
        tab5.setText("资源");
        tab5.setMinWidth(80);
        tab5.setPadding(12, 0, 12, 0);
        tabList.addTab(tab5, 1); // 1 表示位置，在第一个后新增一个

        // 默认选中 某一个tab
        tab2.select();
        tab2.getPosition(); // 获取tab的当前索引下标

        // 设置监听
        tabList.addTabSelectedListener(new TabList.TabSelectedListener() {
            @Override
            public void onSelected(TabList.Tab tab) {
                // 当某个Tab从未选中状态变为选中状态时的回调
                System.out.println("名称："+tab.getText()+"- 下标："+tab.getPosition());
                text_title.setText(tab.getText());
            }

            @Override
            public void onUnselected(TabList.Tab tab) {
                // 当某个Tab从选中状态变为未选中状态时的回调
            }

            @Override
            public void onReselected(TabList.Tab tab) {
                // 当某个Tab已处于选中状态，再次被点击时的状态回调
            }
        });
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
